package com.totcy.atom.utils.weather;

import com.totcy.atom.R;

import java.util.Calendar;

/**
 * description : 天气代码转天气图标
 *
 * @author : Totcy
 * @date : 2018/1/30 16:29
 * @update : 2018/1/30
 */

public class WeatherConvert {

    public static final int WCODE_SUNNY = 1;//晴 100
    public static final int WCODE_PARTLY_CLOUDY = 2;//晴转多云 103
    public static final int WCODE_CLOUDY = 3;//多云 101
    public static final int WCODE_FEW_CLOUDS = 4;//少云 102
    public static final int WCODE_OVERCAST = 5;//阴 104
    public static final int WCODE_WINDY = 6;//有风 200-204
    public static final int WCODE_STRONG_BREEZE = 7;//强风 205-207
    public static final int WCODE_STRONG_GALE = 8;//裂风 208-213
    public static final int WCODE_SHOWER_RAIN = 9;//阵雨 300-301
    public static final int WCODE_THUNDERSHOWER = 10;//雷阵雨 302
    public static final int WCODE_HEAVY_THUNDERSTORM = 11;//强雷阵雨 303-304
    public static final int WCODE_LIGHT_RAIN = 12;//小雨/毛毛雨 305/309
    public static final int WCODE_MODERATE_RAIN = 13;//中雨 306
    public static final int WCODE_HEAVY_RAIN = 14;//大雨 307
    public static final int WCODE_EXTREME_RAIN = 15;//极端降雨/暴雨/大暴雨/特大暴雨 308/310/311/312  313(冻雨)未处理
    public static final int WCODE_LIGHT_SNOW = 16;//小雪 400
    public static final int WCODE_MODERATE_SNOW = 17;//中雪 401
    public static final int WCODE_HEAVY_SNOW = 18;//大雪 暴雪402-403
    public static final int WCODE_SLEET = 19;//雨夹雪 雨雪天气 404-405
    public static final int WCODE_SHOWER_SNOW = 20;//阵雨夹雪 406-407
    public static final int WCODE_FOGGY = 21;//薄雾 雾500-501
    public static final int WCODE_HAZE = 22;//霾 502
    public static final int WCODE_SAND = 23;//扬沙 503
    public static final int WCODE_DUST = 24;//浮尘 504
    public static final int WCODE_DUSTSTORM = 25;//沙尘暴 507-508
    public static final int WCODE_HOT = 26;//冷 900
    public static final int WCODE_COLD = 27;//热 901
    public static final int WCODE_UNKNOWN = 28;//未知 999

    /**
     * 天气代码转本地代码
     *
     * @param weatherCode
     * @return
     */
    public static WeatherIconBean weatherCode2Local(String weatherCode) {
        WeatherIconBean weatherIconBean = new WeatherIconBean(R.drawable.weizhi, WCODE_UNKNOWN);
        switch (weatherCode) {

            case "100":
                weatherIconBean.setIconRes(R.drawable.qing);
                weatherIconBean.setLocalCode(WCODE_SUNNY);
                break;
            case "101":
                weatherIconBean.setIconRes(R.drawable.duoyun);
                weatherIconBean.setLocalCode(WCODE_CLOUDY);
                break;
            case "102":
                weatherIconBean.setIconRes(R.drawable.shaoyun);
                weatherIconBean.setLocalCode(WCODE_FEW_CLOUDS);
                break;
            case "103":
                weatherIconBean.setIconRes(R.drawable.qingjianduoyun);
                weatherIconBean.setLocalCode(WCODE_PARTLY_CLOUDY);
                break;
            case "104":
                weatherIconBean.setIconRes(R.drawable.yintian);
                weatherIconBean.setLocalCode(WCODE_OVERCAST);
                break;
            case "200":
            case "201":
            case "202":
            case "203":
            case "204":
                weatherIconBean.setIconRes(R.drawable.youfeng);
                weatherIconBean.setLocalCode(WCODE_WINDY);
                break;
            case "205":
            case "206":
            case "207":
                weatherIconBean.setIconRes(R.drawable.qiangfeng);
                weatherIconBean.setLocalCode(WCODE_STRONG_BREEZE);
            case "208":
            case "209":
            case "210":
            case "211":
            case "212":
            case "213":
                weatherIconBean.setIconRes(R.drawable.liefeng);
                weatherIconBean.setLocalCode(WCODE_STRONG_GALE);
                break;
            case "300":
            case "301":
                weatherIconBean.setIconRes(R.drawable.zhenyu);
                weatherIconBean.setLocalCode(WCODE_SHOWER_RAIN);
                break;
            case "302":
                weatherIconBean.setIconRes(R.drawable.leizhenyu);
                weatherIconBean.setLocalCode(WCODE_THUNDERSHOWER);
                break;
            case "303":
            case "304":
                weatherIconBean.setIconRes(R.drawable.qiangleizhenyu);
                weatherIconBean.setLocalCode(WCODE_HEAVY_THUNDERSTORM);
                break;
            case "305":
            case "309":
                weatherIconBean.setIconRes(R.drawable.xiaoyu);
                weatherIconBean.setLocalCode(WCODE_LIGHT_RAIN);
                break;
            case "306":
                weatherIconBean.setIconRes(R.drawable.zhongyu);
                weatherIconBean.setLocalCode(WCODE_MODERATE_RAIN);
                break;
            case "307":
                weatherIconBean.setIconRes(R.drawable.dayu);
                weatherIconBean.setLocalCode(WCODE_HEAVY_RAIN);
                break;
            case "308":
            case "310":
            case "311":
            case "312":
                weatherIconBean.setIconRes(R.drawable.tedabaoyu);
                weatherIconBean.setLocalCode(WCODE_EXTREME_RAIN);
                break;
            case "400":
                weatherIconBean.setIconRes(R.drawable.xiaoxue);
                weatherIconBean.setLocalCode(WCODE_LIGHT_SNOW);
                break;
            case "401":
                weatherIconBean.setIconRes(R.drawable.zhongxue);
                weatherIconBean.setLocalCode(WCODE_MODERATE_SNOW);
                break;
            case "402":
            case "403":
                weatherIconBean.setIconRes(R.drawable.daxue);
                weatherIconBean.setLocalCode(WCODE_HEAVY_SNOW);
                break;
            case "404":
            case "405":
                weatherIconBean.setIconRes(R.drawable.yujiaxue);
                weatherIconBean.setLocalCode(WCODE_SLEET);
                break;
            case "406":
            case "407":
                weatherIconBean.setIconRes(R.drawable.zhenyujiaxue);
                weatherIconBean.setLocalCode(WCODE_SHOWER_SNOW);
                break;
            case "500":
            case "501":
                weatherIconBean.setIconRes(R.drawable.bowu);
                weatherIconBean.setLocalCode(WCODE_FOGGY);
                break;
            case "502":
                weatherIconBean.setIconRes(R.drawable.mai);
                weatherIconBean.setLocalCode(WCODE_HAZE);
                break;
            case "503":
                weatherIconBean.setIconRes(R.drawable.yangsha);
                weatherIconBean.setLocalCode(WCODE_SAND);
                break;
            case "504":
                weatherIconBean.setIconRes(R.drawable.fuchen);
                weatherIconBean.setLocalCode(WCODE_DUST);
                break;
            case "507":
            case "508":
                weatherIconBean.setIconRes(R.drawable.shachenbao);
                weatherIconBean.setLocalCode(WCODE_DUSTSTORM);
                break;
            case "900":
                weatherIconBean.setIconRes(R.drawable.leng);
                weatherIconBean.setLocalCode(WCODE_HOT);
                break;
            case "901":
                weatherIconBean.setIconRes(R.drawable.re);
                weatherIconBean.setLocalCode(WCODE_COLD);
                break;
            case "999":
                weatherIconBean.setIconRes(R.drawable.weizhi);
                weatherIconBean.setLocalCode(WCODE_UNKNOWN);
                break;
            default:
                break;

        }
        return weatherIconBean;
    }

    public static String getWeatherBackground(String weatherCode, boolean isBlur) {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //白天
        boolean isDay = hour > 6 && hour < 18;
        StringBuilder scene = new StringBuilder("file:///android_asset/s0001/");
        String sceneMiddle = "weather_na";
        String sceneEnd = "/" + (isBlur ? "scene_blur" : "scene_preview") + ".jpg";
        int code = weatherCode2Local(weatherCode).getLocalCode();
        switch (code) {

            case 1:
            case 2:
                sceneMiddle = isDay ? "weather_sunny_day" : "weather_sunny_night";
                break;
            case 3:
            case 4:
                sceneMiddle = isDay ? "weather_cloudy_day" : "weather_cloudy_night";
                break;
            case 5:
                sceneMiddle = "weather_overcast";
                break;
            case 10:
            case 11:
                sceneMiddle = "weather_thunder_storm";
                break;
            case 9:
            case 12:
            case 13:
            case 14:
            case 15:
                sceneMiddle = isDay ? "weather_rain_day" : "weather_rain_night";
                break;
            case 16:
            case 17:
            case 18:
            case 19:
            case 20:
                sceneMiddle = isDay ? "weather_snow_day" : "weather_snow_night";
                break;
            case 21:
                sceneMiddle = isDay ? "weather_fog_day" : "weather_fog_night";
                break;
            case 22:
                sceneMiddle = "weather_haze";
                break;
            case 23:
            case 24:
            case 25:
                sceneMiddle = "weather_sand_storm";
                break;
            default:
                break;

        }
        scene.append(sceneMiddle).append(sceneEnd);
        return scene.toString();
    }

    public static class WeatherIconBean {
        private int iconRes;
        private int localCode;

        public WeatherIconBean() {
        }

        public WeatherIconBean(int iconRes, int localCode) {
            this.iconRes = iconRes;
            this.localCode = localCode;
        }

        public int getIconRes() {
            return iconRes;
        }

        public void setIconRes(int iconRes) {
            this.iconRes = iconRes;
        }

        public int getLocalCode() {
            return localCode;
        }

        public void setLocalCode(int localCode) {
            this.localCode = localCode;
        }
    }
}
